package com.zhuangwei.sell.converter;

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.zhuangwei.sell.dataobject.OrderDetail;
import com.zhuangwei.sell.dto.OrderDto;
import com.zhuangwei.sell.enums.ResultEnum;
import com.zhuangwei.sell.exception.SellException;
import com.zhuangwei.sell.form.OrderForm;
import lombok.extern.slf4j.Slf4j;
import sun.rmi.runtime.Log;

import java.util.ArrayList;
import java.util.List;

/**
 * @author LeviCoder
 * @create 2019-03-02 23:59
 */
@Slf4j
public class OrderForm2OrderDtoConverter {

    public  static OrderDto convert(OrderForm orderForm){
        OrderDto orderDto = new OrderDto();

        orderDto.setBuyerName(orderForm.getName());
        orderDto.setBuyerPhone(orderForm.getPhone());
        orderDto.setBuyerAddress(orderForm.getAddress());
        orderDto.setBuyerOpenid(orderForm.getOpenid());

        Gson gson = new Gson();
        ArrayList<OrderDetail> detailArrayList = new ArrayList<>();
        try {
            detailArrayList = gson.fromJson(orderForm.getItems(), new TypeToken<List<OrderDetail>>() {
            }.getType());
        }catch (Exception e){
            log.error("【对象转化】错误，string={}",orderForm.getItems());
            throw new SellException(ResultEnum.PARAM_ERROR);
        }

        orderDto.setOrderDetailList(detailArrayList);

        return orderDto;
    }
}
